package com.peanut.baby.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.peanut.baby.R;
import com.peanut.devlibrary.media.AudioPlayer;
import com.peanut.devlibrary.util.SysUtil;

/**
 * Created by anonymouslystudio on 2017/7/17.
 */

public class VoicePlayView extends LinearLayout{

    private int maxVoiceDuration = 60;

    private FrameLayout container;
    private ImageView   icon;
    private TextView    duration;

    private Handler     uiHandler;

    private AnimationDrawable   drawable;

    public VoicePlayView(Context context) {
        super(context);
        init();
    }

    public VoicePlayView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VoicePlayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        View root = LayoutInflater.from(getContext()).inflate(R.layout.view_layout_audio_play,null);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,dip2px(getContext(),40));
        root.setLayoutParams(params);
        addView(root);
        container = (FrameLayout) root.findViewById(R.id.container);
        icon = (ImageView) root.findViewById(R.id.play_icon);
        duration = (TextView) root.findViewById(R.id.play_duration);

        icon.setImageResource(R.drawable.voice_from_icon_white);
        drawable = (AnimationDrawable) icon.getDrawable();

        if(drawable.isRunning()){
            drawable.stop();
        }

    }

    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private void startAnim(){
        post(new Runnable() {
            @Override
            public void run() {
                drawable.start();
            }
        });
    }

    private void stopAnim(){
        post(new Runnable() {
            @Override
            public void run() {
                drawable.stop();
                icon.setImageResource(R.drawable.voice_from_icon_white);
                drawable = (AnimationDrawable) icon.getDrawable();
            }
        });
    }

    public void setDuration(int dur){
        duration.setText(dur+"``");
        fixViewWidthWithDuration(dur);
    }

    private void fixViewWidthWithDuration(int dur){
        if(dur <= 0){
            dur = 10;
        }
        if(dur > maxVoiceDuration){
            dur = 60;
        }
        int width = getWidth()*dur / maxVoiceDuration;
        int minWidth = SysUtil.dip2px(100);
        if(width < minWidth){
            width = minWidth;
        }
        LayoutParams params = (LayoutParams) container.getLayoutParams();
        params.width = width;
        container.setLayoutParams(params);

    }


    public void play(String url){
        AudioPlayer.getInstance().setDataSource(getContext(), Uri.parse(url));
        AudioPlayer.getInstance().setOnAudioPlayListener(new AudioPlayer.OnAudioPlayListener() {
            @Override
            public void onReset() {

            }

            @Override
            public void onError(String reason) {
                showError(reason);
            }

            @Override
            public void onPlay(long currentPos) {

            }

            @Override
            public void onComplete() {
                stopAnim();
            }

            @Override
            public void onPrepared(long totalTime) {
                startAnim();
            }
        });

        AudioPlayer.getInstance().play();
    }

    public boolean isPlaying(){
        return AudioPlayer.getInstance().isPlaying();
    }

    public void stopPlay(){
        stopAnim();
        AudioPlayer.getInstance().releasePlayer();
    }

    private void showError(final String reason){
        post(new Runnable() {
            @Override
            public void run() {
                duration.setText(reason);
            }
        });
    }


    private void onDestory(){

    }


}
